package com.szrt.java.day20170113.demo02;

import java.lang.reflect.Field;

import org.junit.Test;

public class FieldTest {

	/**
	 * new出来学生 card的默认值0 通过反射直接访问private属性，并且改变它的值然后拿出来看一下
	 * @throws Exception 
	 */
	@Test
	public void fieldTest() throws Exception{
		
		@SuppressWarnings("rawtypes")
		Class clazz = Class.forName("com.szrt.java.day20170113.demo01.Student");
		
		Object obj = clazz.newInstance();
		
		Field field = clazz.getDeclaredField("card");
		
		//打开private属性的访问权限
		field.setAccessible(true);
		
		System.out.println("拿到student的Private属性："+field.get(obj));
		
		field.set(obj, 666);
		System.out.println("拿到修改后student的Private属性："+field.get(obj));
		
	}
	
}
